package com.lpb.spring.test1.factory.support;

import com.lpb.spring.test1.factory.BeanFactory;
import com.lpb.spring.test1.factory.config.BeanDefinition;
import com.lpb.spring.test1.factory.config.DefaultSingletonBeanRegistry;

/**
 * @author linpengbo
 * @date 2022/4/27
 */
public abstract class  AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) {
        Object singleton = getSingleton(beanName);
        if(singleton!=null){
          return singleton;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName,beanDefinition,null);
    }

    @Override
    public Object getBean(String beanName, Object... agrs) {
        Object singleton = getSingleton(beanName);
        if(singleton!=null){
            return singleton;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName,beanDefinition,agrs);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName);

    protected abstract Object createBean(String beanName,BeanDefinition definition,Object[] args);

}
